package com.ruoyi.system.service.impl;

import com.ruoyi.system.mapper.AddressMapper;
import com.ruoyi.system.domain.Address;
import com.ruoyi.system.service.AddressService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Address)表服务实现类
 *
 * @author makejava
 * @since 2020-11-18 13:59:26
 */
@Service("addressService")
public class AddressServiceImpl implements AddressService {
    @Resource
    private AddressMapper addressDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Address queryById(Integer id) {
        return this.addressDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param userId 用户Id
     * @return 对象列表
     */
    @Override
    @Cacheable(value = "douyin:user:address", key = "#p0")
    public List<Address> queryAllByLimit(String userId) {
        return this.addressDao.queryAllByLimit(userId);
    }

    /**
     * 新增数据
     *
     * @param address 实例对象
     * @return 实例对象
     */
    @Override
    public Address insert(Address address) {
        this.addressDao.insert(address);
        return address;
    }

    /**
     * 修改数据
     *
     * @param address 实例对象
     * @return 实例对象
     */
    @Transient
    @Override
    @CacheEvict(value = "douyin:user:address", key = "#p0.userId")
    public Address update(Address address) {
        if (address.getDefalut() == 1) {
            addressDao.updateBatch(Integer.parseInt(address.getUserId()));
        }
        this.addressDao.update(address);
        return this.queryById(address.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.addressDao.deleteById(id) > 0;
    }

    /**
     * 获取默认方法
     */
    @Override
    public Address getDefault(Integer userId) {
        return addressDao.getDefault(userId);
    }
}
